#!/bin/sh

sudo -v

# openSUSE
{{ if eq .chezmoi.osRelease.id "opensuse-tumbleweed" }}
# repositories
if ! zypper repos | grep -q packman; then
    sudo zypper addrepo -cf -p 90 https://ftp.gwdg.de/pub/linux/misc/packman/suse/openSUSE_Tumbleweed/ packman
    sudo zypper addrepo -cf https://download.opensuse.org/repositories/home:yacar/openSUSE_Factory/home:yacar.repo
    sudo zypper --gpg-auto-import-keys refresh
fi

# packages
sudo zypper -n install {{ concat .packages.common .packages.opensuse | join " " }}
sudo zypper -n install --no-recommends {{ .packages.opensuse_no_rec }}

# services
if ! systemctl is-active -q tuned; then
    sudo systemctl enable --now tuned
fi

# network
if [ ! -f /etc/NetworkManager/conf.d/dns.conf ]; then
    printf "%s\n" "[global-dns-domain-*]" "servers={{ .dns.nameservers | join "," }}" |
        sudo tee /etc/NetworkManager/conf.d/dns.conf
fi

# directories
mkdir -p {{ .directories.home | join " " }}
{{ end }}

# FreeBSD
{{ if eq .chezmoi.os "freebsd" }}
# packages
sudo pkg install -y {{ .packages.common | join " " }}

# services
sudo sysrc {{ .services.freebsd | join " " }}

# network
if ! grep -q prepend /etc/dhclient.conf; then
    printf "%s\n" "prepend domain-name-servers {{ .dns.nameservers | join ", " }};" |
        sudo tee -a /etc/dhclient.conf
fi
{{ end }}

# shell
if ! getent passwd "$(id -u)" | grep -q zsh; then
   sudo chsh -s "$(command -v zsh)" "$(id -nu)"
fi
